/* eslint-disable no-undef */
import tool from "@/utils/tool";
export default {
	head: function (
		msgType,
		userCode,
		userName,
		orgCode,
		orgName,
		sessionId,
		token,
		takeDataType,
		remoteOrgCode,
		noSync,
		isTrace
	) {
		(this.MsgType = msgType ? msgType : "query"),
			(this.UserCode = userCode ? userCode : ""),
			(this.UserName = userName ? userName : ""),
			(this.OrgCode = orgCode ? orgCode : ""),
			(this.OrgName = orgName ? orgName : ""),
			(this.SessionId = sessionId ? sessionId : ""),
			(this.Token = token ? token : ""),
			(this.TakeDataType = takeDataType ? takeDataType : "local"),
			(this.RemoteOrgCode = remoteOrgCode ? remoteOrgCode : ""),
			(this.NoSync = noSync ? noSync : false),
			(this.IsTrace = isTrace ? isTrace : false);
	},

	entitydata: function (head) {
		this.Data = {
			Head: head,
			Content: { ObjectList: { UseTS: false, Object: [] } },
		};
	},

	entityobj: function (
		objectName,
		orgCode,
		doType,
		doTypeDescribe,
		useDistinct,
		useBackUp,
		useGroup,
		valueList,
		queryList,
		orderList,
		groupList,
		pageSize,
		pageIndex,
		subObjectList
	) {
		(this.AttrList = {
			Name: objectName,
			OrgCode: orgCode,
			DoType: doType,
			DoTypeDescribe: doTypeDescribe,
			UseDistinct: useDistinct,
			UseBackUp: useBackUp,
			UseGroup: useGroup,
			SyncOrgCode: "",
			DataSync: false,
			BeforeSql: "",
			AfterSql: "",
		}),
			(this.ValueList = valueList),
			(this.QueryList = queryList),
			(this.OrderList = orderList),
			(this.GroupList = groupList),
			(this.PageSize = pageSize),
			(this.PageIndex = pageIndex),
			(this.UseGroup = useGroup),
			(this.SubObjectList = subObjectList);
	},

	getProperty: function (
		objectName,
		Property,
		queryList,
		aggregate,
		useCache
	) {
		var endata = new entitydata();
		initHead(endata);
		endata.Data.Head.MsgType = "getprop";
		var eo = new entityobj();
		eo.ObjectName = objectName;
		eo.Property = Property;
		eo.UseCache = useCache;
		eo.Aggregate = aggregate;
		eo.QueryList = queryList;
		endata.Data.Content.ObjectList.Object.push(eo);
		var returnValue = this.$API.auth.objProcess.post(data);
		if (returnValue) {
			return returnValue.text();
		}
	},

	getObjectProperty: function (objectName, valueList, queryList) {
		var endata = new entitydata();
		initHead(endata);
		endata.head.msgType = "getobjprop";
		var eo = new entityobj();
		eo.objectName = objectName;
		eo.valueList = valueList;
		eo.queryList = queryList;
		endata.content.objectList.object.push(eo);
		var returnValue = this.$API.auth.objProcess.post(data);
		if (returnValue) {
			return returnValue.text();
		}
	},

	entitydataProcess: function (
		msgType,
		objectName,
		valueList,
		queryList,
		orderList,
		groupList
	) {
		var endata = new entitydata();
		initHead(endata, msgType);
		var eo = new entityobj();
		eo.objectName = objectName;
		eo.Property = Property;
		eo.useCache = useCache;
		eo.aggregate = aggregate;
		eo.valueList = valueList;
		eo.queryList = queryList;
		eo.OrderList = orderList;
		eo.GroupList = groupList;
		endata.content.objectList.object.push(eo);
		var returnValue = this.$API.auth.objProcess.post(data);
		if (returnValue) {
			return returnValue.text();
		}
	},

	initHead: function (head, msgType) {
		let ui = tool.data.get("USER_INFO");
		head.MsgType = msgType;
		head.UserCode = ui.userCode;
		head.UserName = ui.userName;
		head.OrgCode = ui.orgCode;
		head.OrgName = ui.orgName;
		head.SessionId = ui.sessionid;
		head.Token = ui.token;
		head.TakeDataType = "local";
		head.RemoteOrgCode = "";
		head.NoSync = false;
		head.IsTrace = false;
	},
	//批量修改的返回的类型
	resultData: function (
		code,
		dataType,
		dataFormat,
		value,
		exendType,
		exendState,
		exendValue,
		exendSign,
		exend1,
		exend2,
		exend3
	) {
		this.Code = code; //字段
		this.DataType = dataType; //数据类型
		this.DataFormat = dataFormat ?? ""; //时间格式
		this.Value = value; //直接要修改的值
		this.ExendType = exendType ?? ""; //追加（只针对字符串有效append）还是覆盖cover，对DataType='String'有效
		this.ExendState = exendState ?? false; //是否启用了扩展功能，只针对Number或DateTime有效
		this.ExendValue = exendValue ?? ""; //动态修改的值
		this.ExendSign = exendSign ?? ""; //针对时间是（+-），针对数字是（+-*/）
		this.Exend1 = exend1 ?? false; //数字修改的话是否启用%
		this.Exend2 = exend2 ?? false; //数字修改的话是否保留整数
		this.Exend3 = exend3 ?? ""; //时间格式问题(datetime,date,time)
	},
	batchData: function () {
		rownumber = 1; //行号
		Code = "Name"; //代码（不显示）
		Name = "名称"; //名称(显示)
		DataType = "String"; //数据类型String，Array，Dictionary，Josn，Number，DateTime，Bool
		DataFormat = ""; //时间格式 date time:YYYY-MM-DD HH:mm:ss
		Value = "";
		Source = null; //数据源
		ExendType = "append"; //是覆盖还是追加
		ExendState = false; //启用扩展
		ExendValue = ""; //扩展值
		ExendSign = "";
		Exend1 = false;
		Exend2 = false;
		Exend3 = "";
	},
	transQuery(queryList, selectKeyValue = "OrgCode") {
		// eslint-disable-next-line no-debugger
		//debugger;
		let newquery = {};
		// for (let key in queryList)  {
		// 	if (
		// 		Array.isArray(queryList[key]) &&
		// 		queryList[key + "_Start"] &&
		// 		queryList[key + "_End"]
		// 	) {
		// 		newquery[key + "_Start"] = queryList[key][0];
		// 		if (queryList[key].length > 1)
		// 			newquery[key + "_End"] = queryList[key][1];
		// 	} else newquery[key] = queryList[key];
		// }
		for (let key in queryList) {
			//alert(key)
			if(!queryList[key]){
				continue
			}
			if (Array.isArray(queryList[key])) {
				if (
					typeof queryList[key + "_Start"] != "undefined" ||
					typeof queryList[key + "_End"] != "undefined"
				) {
					if (typeof queryList[key + "_Start"] != "undefined")
						newquery[key + "_Start"] = queryList[key][0];
					if (typeof queryList[key + "_End"] != "undefined")
						newquery[key + "_End"] = queryList[key][1];
				} else if (
					key.lastIndexOf("List") >= 0 &&
					queryList[key.replace("List", "")] != undefined
				) {
					let codelist = "";
					for (let j = 0; j < queryList[key].length; j++)
						if(key.includes(selectKeyValue)){
							codelist += queryList[key][j][selectKeyValue] + ",";
						}else{
							codelist += queryList[key][j] + ",";
						}
					newquery[key.replace("List", "")] = codelist.trim(",");
				} else {
					//newquery[key] = queryList[key].toString();
					let list = "";
					for (let subkey in queryList[key]) {
						list += "," + queryList[key][subkey][key];
					}
					newquery[key] = list.trim(",");
				}
			} else if (queryList[key].length == 0) {
				//if (key.indexOf("_") > 0) newquery[key] = queryList[key];
				continue;
			} else {
				newquery[key] = queryList[key];
			}
		}

		return newquery;
	},
	getGuid: function (count) {
		let realCount = 1;
		try {
			if (count && Number(count)) realCount = Number(count);
		} catch {
			e;
		}
		var result = new Array();
		for (let c = 0; c < realCount; c++) {
			let guid = "";
			let prefix11 = ""; //基于时间产生的随机数
			let d = new Date();
			let x = Math.random(); //获取一个0~1之间的随机数
			let y = d.getTime(); //返回 1970 年 1 月 1 日至今的毫秒数
			if (x < 0.1) x += 0.1;
			//根据当前日期，产生GUID的前11位，避免GUID重复，引入一个随机数
			prefix11 = Math.round(x * y * 10).toString(16);
			guid += prefix11.substring(0, 8);
			guid += "-";
			guid += prefix11.substring(8, 11);
			//后面的数据位，完全采用随机数产生
			for (var i = 12; i <= 32; i++) {
				var g = Math.floor(Math.random() * 16).toString(16);
				guid += g;
				if (i == 12 || i == 16 || i == 20) {
					guid += "-";
				}
			}
			result.push(guid);
		}
		return result;
	},
};
